<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="成果标题" prop="noticeTitle">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入成果标题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-row v-for="item in resultList" :key="item.id" class="list-row">
        <el-col :span="24">
          <el-row class="title">
            <el-col :span="20">{{ item.name }}</el-col>
            <el-col :span="4">
              <!-- <el-button link type="primary" icon="Download" @click="download(item)"
                >下载</el-button
              > -->

              <el-button link type="primary" icon="Reading" @click="preview(item)"
                >在线预览</el-button
              >
            </el-col>
          </el-row>

          <el-row class="intro" :gutter="28"
            ><el-col :span="5">阶段名称：{{ item.missionName }}</el-col
            ><el-col :span="5">上传时间：{{ item.createTime }}</el-col>
            <el-col :span="5">上传人：{{ item.realName }}</el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
  <PreviewDialog v-model="dialogVisible" :infor="currentInfor"></PreviewDialog>
</template>

<script setup name="Information">
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/system/notice";
import { getResultListCommon, addTask } from "@/api/pms/project";
import PreviewDialog from "./previewDialog.vue";
const props = defineProps({
  projectId: {
    type: String,
  },
});
const route = useRoute();
const router = useRouter();

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const resultList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dialogVisible = ref(false);
const currentTaskId = ref("");
const currentInfor = ref({});

const data = reactive({
  form: {},
  queryParams: {
    pageDomain: {
      isAsc: "true",
      orderByColumn: "",
      pageNum: 1,
      pageSize: 10,
      reasonable: true,
    },
    id: "",
    name: "",
  },
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  getResultListCommon(queryParams.value).then((response) => {
    resultList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageDomain.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  // open.value = true;
  // title.value = "添加公告";
  currentTaskId.value = "";
  dialogVisible.value = true;
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  currentTaskId.value = row.id;
  dialogVisible.value = true;
}

/**跳转活动详情 */
function goDetail(row) {
  reset();
  // const noticeId = row.noticeId || ids.value;
  // getNotice(noticeId).then((response) => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改公告";
  // });
  router.push({ path: `detail/${row.id}` });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function submit(formData) {
  addTask({ ...formData, stageId: props.projectId }).then((response) => {
    proxy.$modal.msgSuccess("新增成功");
    getList();
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 下载文件 */
function download(item) {
  proxy.download(
    "/common/download",
    {
      fileName: item.url.replace("/profile/upload", ""),
      delete: false,
    },
    item.name
  );
}
//打开在线预览弹窗
function preview(item) {
  currentInfor.value = item;
  dialogVisible.value = true;
}
// onMounted(() => {
//   getList();
// });

watch(
  () => props.projectId,
  (newId, oldId) => {
    if (newId) {
      queryParams.value.id = props.projectId;
      getList();
    }
  },
  { deep: true, immediate: true }
);
</script>

<style scoped lang="scss">
.list-row {
  padding: 10px 0;
  margin: 10px 0;
  border-bottom: 1px dashed #cccccc;
  .title {
    padding: 10px 0;
  }
  .intro {
    color: #cccccc !important;
  }
}
</style>